<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\TicketType as TicketTypeModel;
use app\common\Logs;
class TicketType extends BaseController{    
    public function __construct(TicketTypeModel $model){
        parent::__construct();
        $this->model = $model;
    }
    public function allList(){
        $params = $this->request->param();
        $data = $this->model->select();
        return $data;
    }
    //添加
    public function index(){        
        $params = $this->request->param();  
        $where = [];
        $page = 1;
        $pageCount = 10;
        //模糊查询           
        if(!empty($params['keyword'])){
            $where['title'] = ['like',"%{$params['keyword']}%"];               
        }      
        //页码            
        $page = !empty($params['page']) ? $params['page'] : $page;
        $pageCount = !empty($params['page_count']) ? $params['page_count'] : $pageCount;

        $datas = $this->model->where($where)->page("$page,$pageCount")->order('id', 'desc')->select();
        $dataCount =  $this->model->where($where)->count();
        $datalist = array(
            'datas' => $datas,            
            'dataCount' => $dataCount
        );
        return $datalist;
    }
    //添加
    public function create(){        
        $param = $this->request->param();  
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);             
        $data = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
        if($data){
            Logs::add("新增现票分类记录",$operator_id,"现票分类", null,json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
    //编辑
    public function update(){        
        $param = $this->request->param();  
        $params = json_decode($param['params'],true);        
        $operator_id = json_decode($param['operator_id'],true);
        $old = $this->model->where("id",$params['id'])->find();
        $data = $this->model->allowField(true)->isUpdate(true)->save($params,['id',$params['id']]);
        if($data){
            Logs::add("修改现票分类记录",$operator_id,"现票分类", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $this->model;
    }
    //删除
    public function delete(){
        $params = $this->request->get();     
        $old = $this->model->where("id",$params['id'])->find();        
        $data = $this->model->allowField(true)->where('id',$params['id'])->delete();
        if($data){
            Logs::add("删除现票分类记录",$params['operator_id'],"现票分类", json_encode($old,JSON_UNESCAPED_UNICODE),json_encode($this->model,JSON_UNESCAPED_UNICODE));    
        }
        return $data;
    }

}